package com.pinyougou.sellergoods.service.impl;

import com.alibaba.dubbo.config.annotation.Service;
import com.github.pagehelper.PageHelper;
import com.github.pagehelper.PageInfo;
import com.pinyougou.service.impl.BaseServiceImpl;
import com.pinyougou.mapper.BrandMapper;
import com.pinyougou.pojo.TbBrand;
import com.pinyougou.sellergoods.service.BrandService;
import org.apache.commons.lang3.StringUtils;
import org.springframework.beans.factory.annotation.Autowired;
import tk.mybatis.mapper.entity.Example;


import java.util.List;
import java.util.Map;

@Service
public class BrandServiceImpl extends BaseServiceImpl<TbBrand> implements BrandService {

    @Autowired
    private BrandMapper brandMapper;//这个接口不继承baseMapper可以嘛

    public List<TbBrand> queryAll() {
        return brandMapper.queryAll();
    }

    @Override
    public PageInfo<TbBrand> findPage(int pageNum, int pageSize) {
        PageHelper.startPage(pageNum, pageSize);
        List<TbBrand> list = brandMapper.selectAll();

        return new PageInfo<TbBrand>(list);
    }

    @Override
    public PageInfo<TbBrand> search(int pageNum, int pageSize, TbBrand tbBrand) {
        //设置分页
        PageHelper.startPage(pageNum, pageSize);
        //构造查询条件
        Example example = new Example(TbBrand.class);
        Example.Criteria criteria = example.createCriteria();

        //首字母
        if (StringUtils.isNotBlank(tbBrand.getFirstChar())) {
            criteria.andEqualTo("firstChar", tbBrand.getFirstChar());

        }

        //品牌名称
        if (StringUtils.isNotBlank(tbBrand.getName())) {
            criteria.andLike("name", "%" + tbBrand.getName() + "%");

        }

        return new PageInfo<>(brandMapper.selectByExample(example));
    }

    /**
     * 获取格式化的品牌列表；格式为：[{id:'1',text:'联想'},{id:'2',text:'华为'}]
     * @return 品牌列表；格式为：[{id:'1',text:'联想'},{id:'2',text:'华为'}]
     */
    @Override
    public List<Map<String, Object>> selectOptionList() {

        return brandMapper.selectOptionList();
    }
}
